package com.application.fabric.users.service;


import com.application.fabric.fabriccontrol.fabricinit.register.RegisterUser;
import com.application.fabric.users.config.encryptAl.EncrypAl;
import com.application.fabric.users.config.encryptAl.ReadKey;
import com.application.fabric.users.entity.LoginSuccessmsg;
import com.application.fabric.users.entity.Usermessage;
import com.application.fabric.users.mapper.usermessagemapper;
import com.application.fabric.users.service.impl.ILoginusers;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.Iterator;
import java.util.List;

@Service
public class SeLoginusers implements ILoginusers {
    @Autowired
    private usermessagemapper usemsg;

    /*其实这个只是查询全部信息*/
    @Override
    public List<Usermessage> loginve() {
        return usemsg.loginver();
    }


    /*普通登陆
    * 成功的hua返回msg为提示信息
    * privatekey为私自密钥
    * publickey为公密钥
    * 放在
    * */
    @Override
    public LoginSuccessmsg commonlogin(Usermessage usermessage) {
        /*查询全部信息 验证是否出现过手机号
         * 若是没有返回错误信息
         * 若是有则不进行验证
         * 返回公钥与私钥
         * */
//        返回信息用LoginSuccessmsg包裹
        LoginSuccessmsg returnmsg = new LoginSuccessmsg();
        /*1.查询全部信息
         * */
        List<Usermessage>  allmsg = loginve();
        /*2.验证手机号
         * */
        Iterator<Usermessage> it = allmsg.iterator();
        while (it.hasNext()){
            Usermessage tem = it.next();
            if (usermessage.getPhonenum().equals(tem.getPhonenum()) && usermessage.getPassword().equals(tem.getPassword())){
                /*已经存在验证成功
                 * */
                returnmsg.setPublickey(tem.getPublickey());
                returnmsg.setPrivatekey(tem.getPrivatekey());
                returnmsg.setPhonenum(tem.getPhonenum());
                returnmsg.setUserid(tem.getUserid());
                return returnmsg;
            }
        }
        return returnmsg;
    }

    /*wxlogin
    * */
    public String wxlogin(Usermessage usermessage) throws Exception {
        /*查询全部信息 验证是否出现过手机号
        * 若是没有进行注册 返回公钥与私钥
        * 若是有则不进行验证
        * */
        /*1.查询全部信息
        * */
        List<Usermessage>  allmsg = loginve();
        /*2.验证手机号 and password
        * */
        Iterator<Usermessage> it = allmsg.iterator();
        while (it.hasNext()){
            Usermessage tem = it.next();
            if (usermessage.getPhonenum().equals(tem.getPhonenum())  && usermessage.getPassword().equals(tem.getPhonenum())){
                /*已经存在验证成功
                 * */
                return "验证成功";
            }
        }
        int res = registerve(usermessage);
        if (res == 1){
            System.out.println("注册成功");
            return "注册成功";
        }else {
            return "注册失败";
        }

    }


    /*这个是注册*/
    @Autowired
    private EncrypAl encrypAl;
    @Autowired
    private RegisterUser registerUser;
    @Autowired
    private ReadKey readKey;
    @Override
    public int registerve(Usermessage msg) throws Exception {
        /*先调用查询接口验证是否重名
        * */
        /*默认不重名
         * */
        int flag =1;
        /*true 表示真实存在 false表示假的删除*/
        Boolean status = true;
        List<Usermessage> usermessages = usemsg.loginver();
        Iterator<Usermessage> it = usermessages.iterator();
        while (it.hasNext()){
            Usermessage tem = it.next();
            if (tem.getPhonenum().equals(msg.getPhonenum()) && !msg.equals("D")){
                /*重名*/
                flag=0;
            }
            if (tem.getPhonenum().equals(msg.getPhonenum()) && msg.equals("D")){
                status = !status;
            }
        }
        if (flag == 1  && status){
            //      md5生成用户名id
            String userId = encrypAl.md5encryt(msg.getPhonenum());
            msg.setUsername(userId);
            msg.setUserid(userId);
//        进行fabric注册
            registerUser.init(userId);
            /*公私密钥获取 并且注入*/
            msg.setPublickey(readKey.Certificatemsg(userId));
            msg.setPrivatekey(readKey.PrivateKeymsg(userId));
            /*获取当前时间戳*/
            Date date = new Date(System.currentTimeMillis());
            msg.setTime(date);
            /*设置状态status A*/
            msg.setStatus("A");
            return usemsg.registeruser(msg);
        }
        /*表示处于假删除*/
        else if (!status && flag==1){
            Date date = new Date(System.currentTimeMillis());
            msg.setTime(date);

            msg.setStatus("R");
            return usemsg.registeruser(msg);
        }
        else {
            return 0;
        }
    }
}
